package com.xd.pre.modules.tpshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xd.pre.common.utils.Result;
import com.xd.pre.modules.tpshop.domain.TpUserAddress;
import com.xd.pre.modules.tpshop.service.ITpUserAddressService;
import com.xd.pre.modules.tpshop.util.CommonFunction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/TpUserAddressAction")
public class TpUserAddressController {


    @Autowired
    private ITpUserAddressService userAddress_biz;


    //【1】 http://localhost:8080/tpshop_server/TpUserAddressAction/saveOrAdd_UserAddressWithParameter?loginUser_id=1&consignee=1&province=1&city=1&district=1&street=1&address=1&mobile=1&is_default=0
//	loginUser_id=1&consignee=1&province=1&city=1&district=1&street=1&address=1&mobile=1&is_default=0   &address_id=1			// 测试已通过！！
    @RequestMapping(value = "/saveOrAdd_UserAddressWithParameter", method = RequestMethod.GET)

    public Result saveOrAdd_UserAddressWithParameter(HttpServletRequest request) {

        String loginUser_id = request.getParameter("loginUser_id"); //

        String consignee = request.getParameter("consignee"); //
        String province = request.getParameter("province"); //
        String city = request.getParameter("city"); //
        String district = request.getParameter("district"); //
        String twon = request.getParameter("street"); //
        String address = request.getParameter("address"); //
        String mobile = request.getParameter("mobile"); //
        String is_default = request.getParameter("is_default"); //

        String address_id = request.getParameter("address_id"); // address_id 不为null 说明是 编辑 收货地址；为null说明是新增 收货地址

        if (!CommonFunction.stringCheckNotNull(loginUser_id) || !CommonFunction.stringCheckNotNull(consignee) || !CommonFunction.stringCheckNotNull(province) ||
                !CommonFunction.stringCheckNotNull(city) || !CommonFunction.stringCheckNotNull(district) || !CommonFunction.stringCheckNotNull(twon) ||
                !CommonFunction.stringCheckNotNull(address) || !CommonFunction.stringCheckNotNull(mobile) || !CommonFunction.stringCheckNotNull(is_default)
        ) {
            Result result0 = new Result();
            result0.setStatus(Result.FAILED);
            result0.setData("接收的paramas中有为null的！请确保参数不为null");
            return result0;
        } else {
            if (null == address_id || "".endsWith(address_id)) { // 代表：添加 收货地址 Integer.parseInt()
                Result result1 = new Result();
                TpUserAddress aa = new TpUserAddress();
                aa.setUserId(Integer.parseInt(loginUser_id));
                aa.setConsignee(consignee);
                aa.setProvince(Integer.parseInt(province));
                aa.setCity(Integer.parseInt(city));
                aa.setDistrict(Integer.parseInt(district));
                aa.setTwon(Integer.parseInt(twon));
                aa.setAddress(address);
                aa.setMobile(mobile);
                aa.setIsDefault(Integer.parseInt(is_default));

                boolean save = userAddress_biz.save(aa);
//                int res = userAddress_biz.insertUserAddress(Integer.parseInt(loginUser_id), consignee,
//                        Integer.parseInt(province), Integer.parseInt(city), Integer.parseInt(district), Integer.parseInt(twon), address, mobile, Integer.parseInt(is_default));
                int res = save ? 1 : -1;
                if (res == 1) { // 0代表insert失败
                    result1.setStatus(Result.SUCCESS);
                    result1.setTipMsg("添加 收货地址成功！");
                } else {
                    result1.setStatus(Result.FAILED);
                    result1.setTipMsg("添加 收货地址失败！");
                }
                result1.setData(res + "; 'address_id' == null的时候 代表：添加 收货地址");
                return result1;
            } else { // address_id 不为null 说明是 编辑 收货地址
                Result result2 = new Result();
                TpUserAddress aa = new TpUserAddress();
                aa.setAddressId(Integer.parseInt(address_id));
                aa.setConsignee(consignee);
                aa.setProvince(Integer.parseInt(province));
                aa.setCity(Integer.parseInt(city));
                aa.setDistrict(Integer.parseInt(district));
                aa.setTwon(Integer.parseInt(twon));
                aa.setAddress(address);
                aa.setMobile(mobile);
                aa.setIsDefault(Integer.parseInt(is_default));
                boolean b = userAddress_biz.updateById(aa);
                int res = b ? 1 : -1;
//                int res = userAddress_biz.updateUserAddress(Integer.parseInt(address_id), consignee,
//                        Integer.parseInt(province), Integer.parseInt(city), Integer.parseInt(district), Integer.parseInt(twon), address, mobile, Integer.parseInt(is_default));
                if (res == 1) { // 0代表update失败
                    result2.setStatus(Result.SUCCESS);
                    result2.setTipMsg("编辑 收货地址成功！");
                } else {
                    result2.setStatus(Result.FAILED);
                    result2.setTipMsg("编辑 收货地址失败！");
                }
                result2.setData(res + "; 'address_id' !=null的时候 代表： 编辑 收货地址");
                return result2;
            }
        }
    }


    //【2】 http://localhost:8080/tpshop_server/TpUserAddressAction/delConsigneeAddressByID?ConsigneeAddressById=1  // 测试已通过！！
    @RequestMapping(value = "/delConsigneeAddressByID", method = RequestMethod.GET)
    public Result delConsigneeAddressByID(HttpServletRequest request) {
        Result res = new Result();
        String ConsigneeAddressById = request.getParameter("ConsigneeAddressById"); //
        if (CommonFunction.stringCheckNotNull(ConsigneeAddressById)) {
            boolean b = userAddress_biz.removeById(ConsigneeAddressById);
            int row = b ? 1 : -1;
//            int row = userAddress_biz.delete_byKeyId(Integer.parseInt(ConsigneeAddressById));//主键是唯一的，不需要 再传入 login_userId

            TpUserAddress ua = userAddress_biz.getById(ConsigneeAddressById);
//            TpUserAddress ua = userAddress_biz.findUserAddress_byKeyId(Integer.parseInt(ConsigneeAddressById));
            if (ua.getIsDefault() == 1) {        //删除的是默认地址，则要把第一个地址设置为默认收货地址
                ua.setIsDefault(1);
                userAddress_biz.updateById(ua);
//                userAddress_biz.update_defaultAddress_AfterDeletePO();
            }
//			else{ //ua.getIsDefault() == 1		//删除的 不是 默认地址 ，则直接删除
//
//			}
            if (row != 0) {  //能走到这一步，证明 (userAddress_biz.update_defaultAddress_AfterDeletePO() )这一步也走完了！
                res.setStatus(Result.SUCCESS);
                res.setTipMsg("删除成功");
            } else {
                res.setStatus(Result.FAILED);
                res.setTipMsg("删除失败");
            }
        }

        return res;
    }

    //【3】 http://localhost:8080/tpshop_server/TpUserAddressAction/getTpUserAddressList_byLoginUserId?loginUserId=1  // 测试已通过！！
    @RequestMapping(value = "/getTpUserAddressList_byLoginUserId", method = RequestMethod.GET)
    public Result getTpUserAddressList_byLoginUserId(HttpServletRequest request) {
        Result res = new Result();
        String theUser_id = request.getParameter("loginUserId"); //
        if (CommonFunction.stringCheckNotNull(theUser_id)) {
            LambdaQueryWrapper<TpUserAddress> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.eq(TpUserAddress::getUserId, theUser_id);
            List<TpUserAddress> address_list = userAddress_biz.list(lambdaQuery);
//            List<TpUserAddress> address_list = userAddress_biz.getTpUserAddressList_byLoginUserId(Integer.parseInt(theUser_id));
            if (CommonFunction.listCheckNotNull(address_list)) {
                res.setData(address_list);
                res.setStatus(Result.SUCCESS);
                res.setTipMsg("查询TpUserAddressList成功");
            } else {
                res.setData(address_list);
                res.setStatus(Result.FAILED);
                res.setTipMsg("查询TpUserAddressList失败");
            }
        }
        return res;
    }

}
